import request from '@/utils/request'

// 跟进记录列表响应接口
export interface FollowupListResponse {
  list: Followup[]
  total: number
  page: number
  limit: number
}

// 跟进记录类型定义
export interface Followup {
  id: number
  customer_id: number
  opportunity_id: number
  followup_type: number
  content: string
  next_followup_time: number
  user_id: number
  user_name: string
  create_time: number
  update_time: number
  // 关联字段
  customer_name?: string
  opportunity_title?: string
}

// 跟进记录搜索参数
export interface FollowupSearchParams {
  page?: number
  limit?: number
  customer_id?: number
  opportunity_id?: number
  user_id?: number
  followup_type?: number
  customer_name?: string
  start_time?: number
  end_time?: number
}

// 选项接口
export interface Option {
  value: number
  label: string
}

// 跟进类型选项接口
export interface FollowupTypeOption {
  value: number
  label: string
}

// 跟进记录API
export const followupApi = {
  // 获取跟进记录列表
  list: (params: FollowupSearchParams) => {
    return request({
      url: '/api/crm/followup/list',
      method: 'get',
      params,
    })
  },

  // 获取跟进记录详情
  detail: (id: number) => {
    return request({
      url: `/api/crm/followup/detail/${id}`,
      method: 'get',
    })
  },

  // 新增跟进记录
  add: (data: Partial<Followup>) => {
    return request({
      url: '/api/crm/followup/add',
      method: 'post',
      data,
    })
  },

  // 新增跟进记录（create别名）
  create: (data: Partial<Followup>) => {
    return request({
      url: '/api/crm/followup/add',
      method: 'post',
      data,
    })
  },

  // 更新跟进记录
  edit: (data: Partial<Followup> & { id: number }) => {
    return request({
      url: '/api/crm/followup/edit',
      method: 'put',
      data,
    })
  },

  // 删除跟进记录
  del: (data: { id: number }) => {
    return request({
      url: '/api/crm/followup/del',
      method: 'delete',
      data,
    })
  },

  // 获取跟进选项
  getFollowupOptions: () => {
    return request({
      url: '/api/crm/followup/options',
      method: 'get',
    })
  },

  // 获取跟进类型选项
  getFollowupTypeOptions: () => {
    return request({
      url: '/api/crm/followup/typeOptions',
      method: 'get',
    })
  },

  // 获取跟进结果选项
  getFollowupResultOptions: () => {
    return request({
      url: '/api/crm/followup/resultOptions',
      method: 'get',
    })
  },

  // 批量删除跟进记录
  batchDelete: (data: { ids: number[] }) => {
    return request({
      url: '/api/crm/followup/batchDelete',
      method: 'post',
      data,
    })
  },

  // 获取客户的跟进记录
  getCustomerFollowups: (params: { customer_id: number; page?: number; limit?: number }) => {
    return request({
      url: '/api/crm/followup/customerFollowups',
      method: 'get',
      params,
    })
  },

  // 获取销售机会的跟进记录
  getOpportunityFollowups: (params: { opportunity_id: number; page?: number; limit?: number }) => {
    return request({
      url: '/api/crm/followup/opportunityFollowups',
      method: 'get',
      params,
    })
  },

  // 获取用户的跟进记录
  getUserFollowups: (params: { user_id: number; page?: number; limit?: number }) => {
    return request({
      url: '/api/crm/followup/userFollowups',
      method: 'get',
      params,
    })
  },

  // 获取跟进统计
  getFollowupStatistics: (params?: {
    user_id?: number
    department_id?: number
    start_date?: number
    end_date?: number
  }) => {
    return request({
      url: '/api/crm/followup/statistics',
      method: 'get',
      params,
    })
  },
}

export default followupApi
